CN.record = "位赞助商";
var swfu;
var msg=CN;
var ufile = {fid:0,filepath:"",name:""};
var sponsor = {edit_status:"create",uid:site.uid,status:1,useupload:false,avatar:""};
$(function(){
	init();
	mpage.modalfactory = function(data,position){//生成列表内容 //TODO 需要变化的地方
        $(".tablist").empty();
        tablist.position = position;//设置数据所在位置
        var tdValue =  "",
            rowclass = "",
            index = 1,
            td = function(arg,title){
        		if(!arg)arg="-";
	        	if(title){
	        		return "<td title='"+title+"'>"+arg+"</td>";           
	        	}else{
	        		return "<td>"+arg+"</td>";
	        	}
            },
            setButton = function(data){
            	return "<a class='btn btn-info btn-mini' onclick='del(this)' data-name='"+data.cname+"' data-id='"+data.spid+"'>移除</a>";
            };
        for ( var element in data) {
        	var kk =data[element],
             	u_btn = '<button class="btn btn-info btn-mini" onclick="get(this)" type="button" data-id="'+kk.spid+'">'+kk.cname+'</button>';
             	tdValue =td(index)+ 
            	td(u_btn)+
            	td(kk.ename)+ 
            	td(kk.mobile)+
            	td(kk.email)+
            	td(substring(kk.desc,30),kk.desc)+
             	td(kk.sinaweibo)+
             	td(kk.qqweibo)+
             	td(setButton(kk));
               	rowclass = element%2;//行样式
               	index++;
               $(".tablist").append("<tr class='tr"+rowclass+"'>"+tdValue+"</tr>");
        }
    };
    mpage.setext("?eid="+getUrlParam("eid"));
	mpage.init();
}); 


function init(){
	swfupload_configure();//need to be static by vincent20130808
	init_dialog();
	init_upload_configure();
}

init_upload_configure = function(){
	
	$(".uploadpic").click(function(){
		$('#myModal').modal('hide');
		$('#myModal2').modal('show');		
	});
}

init_dialog = function(){
	$("#dialog_create").click(function(){//create & open dialog
		sponsor.edit_status = "create";
		open_dialog();
	});
	$("#dialog_save").click(function(){
		$(this).attr("disabled",true);
		$(".dialogmsg").text("执行操作中...");
		$("#form1 input").each(function(){
			var id = $(this).attr("id");
			if(id)
				sponsor[id] =  $(this).val();
		});
		sponsor.uid = site.uid;
		sponsor.url = site.web+"my/sponsor/"+sponsor.edit_status;//包含create/update操作
		sponsor.desc = $("#desc").val();
		complete = function(data){
			done_method = function(){
				$('#myModal').modal('hide');
				mpage.init();
			};
			if(data.code==1){
				$(".dialogmsg").text("操作成功!");
				setTimeout(done_method, 2000);
				
			}else{
				$('#myModal').modal('hide');
			}			
		}
		G(sponsor,complete);
	});
	
	
	$("#upload_save").click(function(){//upload save btn
		if(ufile.fid!=0){//get img by fid
			cb = function (data){
				if(data.code==1){
					sponsor.avatar = data.data[0]['filepath'];
					var fileimg = data.data[0]['fileimg'];
					$("#sponsorimg").attr("src",site.web+fileimg);//update img
					$(".canceldialog2").click();
				}else{
					alert("ERROR file 404");
					$(".canceldialog2").click();
				}
			}
			var url = site.web+"my/sponsor/getfile";
			var json = {url:url,fid:ufile.fid,uid:site.uid};
			G(json,cb);
		}
	});
	
	$(".canceldialog").click(function(){//cancel
		clear_dialog();
		$('#myModal').modal('hide');
	});
	$(".canceldialog2").click(function(){//cancel
		$('#myModal2').modal('hide');
		$('#myModal').modal('show');
	});
}

clear_dialog = function(){
	$("#form1 input[type=reset]").click();
	$("#dialog_save").attr("disabled",false);
	$(".dialogmsg").text("");
	$("#sponsorimg").attr("src",site.web+"/skin/img/sponsor.png");
}

open_dialog = function(data){
	clear_dialog();
	switch(sponsor.edit_status){
		case "create":
			$(".dialog_status").text("新增");
			break;
		case "update":
			$(".dialog_status").text("更新");
			$.each(data, function(index, value) {
				$("#"+index).val(value);
				//set avatar
				if(index=="avatar"){
					if(value)$("#sponsorimg").attr("src",site.web+"files/thumb/"+value);
					sponsor.avatar = value;
				}
			});
			sponsor.addtime = data.addtime;
			$("#desc").val(data.desc);			
			break;
	}
	$('#myModal').modal('show');
}

get = function(arg){
	var id = $(arg).attr("data-id");
	cb = function (data){
		if(data.code==1){
			sponsor.edit_status = "update";
			open_dialog (data.data);
		}else{
			alert("ERROR sponsor 404");
		}
	}
	var url = site.web+site.module+"/"+"get";
	var json = {url:url,spid:id,uid:site.uid};
	G(json,cb);
}

del = function(arg){
	var name =  $(arg).attr("data-name");
	if(confirm("确认移除"+name+"?")==false)return;
	var id = $(arg).attr("data-id");
	cb = function (data){
		mpage.init();
	}
	var url = site.web+site.module+"/"+"del";
	var json = {url:url,spid:id,uid:site.uid};
	G(json,cb);
}

swfupload_configure = function(){
	//属性赋值[1]
	var furl 	= site.web + "js/swfupload/swfupload.swf",
		uurl 	= site.web + "my/service/upload",
//		uurl 	= site.web + "my/service/upload?uid="+site.uid,
		btnurl 	= site.web + "skin/img/swfupload.png",
		ssid 	= site.ssid,
		uid 	= site.uid;
	//setting设置[2]
	var settings = {
		flash_url :furl ,
		upload_url: uurl,
		post_params: {"PHPSESSID" : ssid,"uid":uid},
		file_size_limit : "1000 MB",
		file_types : "*.*",
		file_types_description : "All Files",
		file_upload_limit : 10,  
		file_queue_limit : 0,
		custom_settings : {
			progressTarget : "fsUploadProgress",
			cancelButtonId : "btnCancel"
		},
		debug:false,

		// Button settings
		button_image_url: btnurl,
		button_width: "60",
		button_height: "25",
		button_placeholder_id: "spanButtonPlaceHolder",
		button_text: '<span class="theFont">浏览</span>',
		button_text_style: ".theFont { font-size: 14px; color:#ffffff; cursor:pointer;}",
		button_text_left_padding: 14,
		button_text_top_padding: 2,
		
		file_queued_handler : fileQueued,
		file_queue_error_handler : fileQueueError,
		file_dialog_complete_handler : fileDialogComplete,
		upload_start_handler : uploadStart,
		upload_progress_handler : uploadProgress,
		upload_error_handler : uploadError,
		upload_success_handler : uploadSuccess,
		upload_complete_handler : uploadComplete,
		queue_complete_handler : queueComplete	
	};
	//new SWFupload[3]
	swfu = new SWFUpload(settings);
}

